package com.artevivo.utilidades;

import java.util.List;
import java.util.Map;

import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;

import com.artevivo.entidades.Matricula;
import com.artevivo.managedBeans.SessionManagedBean;
import com.artevivo.services.ClienteService;

/**
 * Dummy implementation of LazyDataModel that uses a list to mimic a real
 * datasource like a database.
 */

public class LazyClienteDataModel extends LazyDataModel<Matricula> {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

    private List<Matricula> datasource;

    private ClienteService clienteService;
    
    private SessionManagedBean sessionMB;

    public LazyClienteDataModel(List<Matricula> datasource, ClienteService clienteService, SessionManagedBean sessionMB) {
        this.datasource = datasource;
        this.clienteService = clienteService;
        this.sessionMB = sessionMB;
    }

    @Override
    public int getRowCount() {
        return clienteService.clienteCount().intValue();

    }

    @Override
    public Matricula getRowData(String rowKey) {
        for (Matricula cliente : datasource) {
            if (cliente.getIdMatricula().equals(rowKey)) {
                return cliente;
            }
        }

        return null;
    }

    @Override
    public Object getRowKey(Matricula cliente) {
        return cliente.getIdMatricula();
    }

    @Override
    public List<Matricula> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters) {

        filters.put("anyo", sessionMB.getSelectedAnyo());
        return clienteService.carga(first, pageSize, sortField, sortOrder, filters);

    }

    public ClienteService getClienteService() {
        return clienteService;
    }

    public void setClienteService(ClienteService clienteService) {
        this.clienteService = clienteService;
    }
    
    public SessionManagedBean getSessionMB() {
        return sessionMB;
    }

    public void setSessionMB(SessionManagedBean sessionMB) {
        this.sessionMB = sessionMB;
    }

}
